# -*- encoding: utf-8 -*-
##############################################################################
#
##############################################################################

from odoo import api, fields, models, _
from odoo.addons import decimal_precision as dp
from odoo.exceptions import UserError, ValidationError
from odoo.tools import float_round


class stock_move(models.Model):
    _inherit = 'stock.move'

    mrp_standard_use = fields.Float(u'基准用量', default=0.0)
    bom_line_note = fields.Char(u"备注")

    @api.multi
    def action_done(self):
        for move in self:
            produce_quantity_done = 0
            # 取得成品入库数量
            for produce_move in move.raw_material_production_id.move_finished_ids:
                if produce_move.product_id == move.raw_material_production_id.product_id:
                    produce_quantity_done += produce_move.quantity_done
            # 通过成品入库数量取得应投料物料数量
            if produce_quantity_done > 0:
                product_uom_qty = produce_quantity_done / move.raw_material_production_id.bom_id.product_qty * move.bom_line_id.product_qty
                if move.raw_material_production_id:
                    production = move.raw_material_production_id
                    raw_line = None
                    for raw_li in production.compute_lines:
                        if raw_li.product_id == move.product_id:
                            raw_li.quantity_done += move.quantity_done
                            raw_line = raw_li
                    if not raw_line:
                        res = self.env['mrp.production.compute.line'].create({
                            'product_id': move.product_id.id,
                            'product_uom_qty': product_uom_qty,
                            'product_uom_id': move.product_uom.id,
                            'quantity_done': move.quantity_done,
                            'mrp_standard_use': move.mrp_standard_use,
                            'bom_line_id': move.bom_line_id.id,
                            'name': production.id,
                        })
                        raw_line = res
                        # if raw_line.product_uom_qty>0 and raw_line.quantity_done-raw_line.product_uom_qty>0:
                        #     if abs((raw_line.quantity_done-raw_line.product_uom_qty)/raw_line.product_uom_qty)*100>raw_line.feed_rate:
                        #         raise ValidationError(_(u'已投料数量必须在设定的允许投料范围内,上限:' + ' %s.' + u'下限:'+' %s.') % (raw_line.product_uom_qty*(1-raw_line.feed_rate/100),raw_line.product_uom_qty*(1+raw_line.feed_rate/100)))
        return super(stock_move, self).action_done()
        #
        # @api.onchange('move_raw_ids')
        # def onchange_move_raw_ids(self):
        #     self.ensure_one()
        #     if self.move_raw_ids:
        #         title = u'警告！'
        #         message = ""
        #         warning = {}
        #         compute_lines = {}
        #         for raw_line in self.move_raw_ids:
        #             if compute_lines.get(raw_line.product_id, False):
        #                 updat = compute_lines[raw_line.product_id]
        #                 updat['quantity_done'] = updat.get('quantity_done') + raw_line.quantity_done
        #                 compute_lines[raw_line.product_id] = updat
        #
        #             else:
        #                 compute_lines[raw_line.product_id] = {
        #                     'product_uom_qty': raw_line.product_uom_qty,
        #                     'product_uom_id': raw_line.product_uom.id,
        #                     'mrp_standard_use': raw_line.mrp_standard_use,
        #                     'quantity_done': raw_line.quantity_done,
        #                     'bom_line_id': raw_line.bom_line_id.id,
        #                 }
        #         self.compute_lines = None
        #         for key, value in compute_lines.items():
        #             res = self.env['mrp.production.compute.line'].create({
        #                 'product_id': key.id,
        #                 'product_uom_qty': value.get('product_uom_qty'),
        #                 'product_uom_id': value.get('product_uom_id'),
        #                 'quantity_done': value.get('quantity_done'),
        #                 'mrp_standard_use': value.get('mrp_standard_use'),
        #                 'bom_line_id': value.get('bom_line_id', False),
        #                 'name': self._origin.id,
        #             })
        #     return super(mrp_bom, self).write(vals)
        #     if value.get('product_uom_qty')>0 and value.get('quantity_done')>0:
        #         if abs((value.get('quantity_done')-value.get('product_uom_qty'))/value.get('product_uom_qty'))>res.feed_rate:
        #             warning['title'] = u'警告！'
        #             warning['message'] = _(u'已投料数量必须在设定的允许投料范围内,上限:' + ' %s.' + u'下限:'+' %s.') % (res.product_uom_qty*(1-res.feed_rate),res.product_uom_qty*(1+res.feed_rate))
        # return {'warning': warning}
